package com.wt.ai.controller;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import reactor.core.publisher.Flux;

@RestController
public class ChatController {
    @Autowired
    private ChatClient chatClient;

    @GetMapping(value = "/ai/generate")
    public String generate(@RequestParam String message) {
        return chatClient.prompt()
                .user(message)
                .call()
                .content();
    }

    @CrossOrigin(origins = "*")
    @GetMapping(value = "/ai/stream-generate", produces = "text/event-stream")
    public Flux<String> streamGenerate(@RequestParam String message) {
        return chatClient.prompt()
                .user(message)
                .stream()
                .content()
                .concatWith(Flux.just("End"));
    }

}